package com.sw.c.controller;

import com.sw.c.utils.RedisUtils;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;

@RestController
public class SseController {
    @Resource
    private RedisUtils redisUtils;

    @GetMapping(path = "/stream-sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Scheduled(fixedRate =  1000) //每分钟执行一次
    public SseEmitter handleSse() {

        String key = "qh_hydrodynamic_model";
        String keyBak =  "qh_model_CE_BAK";

        Object o = redisUtils.rightPopAndLeftPush(key,keyBak);
        System.out.println("o数据："+o.toString());
/*        SseEmitter emitter = new SseEmitter();

        // 在新线程中发送事件以避免阻塞主线程
        new Thread(() -> {
            try {
                emitter.send("data:"+new Date()); // 发送数据给客户端
                emitter.complete(); // 完成发送
                System.out.println("123");
            } catch (Exception e) {
                emitter.completeWithError(e); // 发送错误
            }
        }).start();*/

        return null;
    }

}